package com.liohi.ExceptionCatch;

import com.liohi.Param.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * @author liohi
 */
@RestControllerAdvice
public class DefaultExceptionHandler {


    private static final Logger logger = LoggerFactory.getLogger(DefaultExceptionHandler.class);

    @ExceptionHandler(MyException.class)
    @ResponseBody
    public R handlerSellerException(MyException e) {
        logger.error("自定义异常:{}", e.getMessage());
        return R.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public R processRuntimeException(RuntimeException e) {
        logger.error("运行时异常:{}", e.getMessage());
        return R.error(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R processException(Exception e) {
        logger.error("系统异常:{}", e.getMessage());
        return R.error(500, e.getMessage());
    }

}
